﻿// ******************************************************************************
// Developed by Oguz Demirel
// Date: January 28, 2011
// ******************************************************************************
using System;
using System.Web.UI.WebControls;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Utilities;
using Microsoft.SharePoint.WebControls;
using System.Web;

namespace Zieglers.CodePlex.SPLocalize
{
    public partial class SelectLanguage : LayoutsPageBase
    {
        #region ..:: FIELDS ::..
        protected Literal litMessages;
        protected DropDownList ddlLanguage;

        #endregion ..:: FIELDS ::..

        #region ..:: PROPERTIES ::..

        //  If you don’t want the default permissions to be checked,
        //  you just need to override RequireDefaultLayoutsRights and return false.
        //protected override bool RequireDefaultLayoutsRights
        //{
        //    get
        //    {
        //        return false;
        //    }
        //}

        // To make sure that the user is site administrator.
        //protected override bool RequireSiteAdministrator
        //{
        //    get
        //    {
        //        return true;
        //    }
        //}

        // If you are creating an application page the uses the ButtonSection control template 
        // you will have a Cancel button. 
        // The target of this Cancel button is controlled using the PageToRedirectOnCancel property. 
        // Just override the property and return a string containing the target of your cancel page.
        //public override string PageToRedirectOnCancel 
        //{ 
        //    get
        //    {
        //        // Just return url where you want to redirect on cancel button click
        //        return "/_layouts/settings.aspx";
        //    }
        //}

        // Checking the default permissions will not do too much good.
        // You might also want to check if user has enough rights to manage alerts, 
        // create groups or check any of the SharePoint permissions to accomplish a specific business use case. 
        // That is where overriding RightsRequired property comes in. 
        // Simply by overriding it and returning all the permissions you need to check, 
        // SharePoint will take care the rest for you by checking the permissions you listed 
        // including the default layouts permissions and authorize the user to access the custom application page 
        // and if checking fails SharePoint redirects the user to an access denied page and let the user login with a different account.
        protected override SPBasePermissions RightsRequired 
        { 
            get { 
                SPBasePermissions permissions = 
                                    base.RightsRequired | 
                                    SPBasePermissions.BrowseUserInfo | 
                                    SPBasePermissions.CreateGroups | 
                                    SPBasePermissions.ManageLists; 
                return permissions; } 
        }

        #endregion ..:: PROPERTIES ::..

        public SelectLanguage()
        {
            // By default, permissions are checked on OnLoadComplete
            // Here we force it to be checked on OnPreInit
            this.RightsCheckMode = RightsCheckModes.OnPreInit;    
        }

        #region ..:: PAGE EVENTS ::..

        protected override void OnLoad(EventArgs e)
        {
            this.CheckCustomRights();

            if (!Page.IsPostBack)
            {
                LoadLanguages();
            }

            // ------------------------------------------------------------------------------
            // IMPORTANT NOTE: Do NOT get Site and Web objects from current SPContext,
            // unless you use them as read-only!!!
            // 
            // If you need to make modifications on Site and Web objects,
            // use them as shown in Option - 1 or Option - 2.
            // USING clause will ensure that these objects are properly disposed.
            // NEVER Dispose SPContext.SPSite and SPWeb objects
            // ------------------------------------------------------------------------------

            
            // ******************************************************
            // HOW TO : use Site and Web objects in using clauses
            // Option - 2
            //using (SPSite site = new SPSite(this.Site.Url))
            //using (SPWeb web = site.OpenWeb(this.Web.ID))
            //{
            //    litMessages.Text += "RootWeb ID: " + site.RootWeb.ID.ToString() + "<br/>";
            //    litMessages.Text += "CurrentWeb ID: " + web.ID.ToString() + "<br/>";

            //}

            
        }

        protected void Language_Selected(object sender, EventArgs e)     
        {
            //Grab the cookie
            HttpCookie cookie = Request.Cookies["glang"];
            if (cookie != null)
            {
                // Set language in cookie
                cookie.Value = ddlLanguage.SelectedValue;
                // Translate
                string jScript = "<script>doGTranslate(\"" + cookie.Value + "\");</script>";
                ClientScript.RegisterClientScriptBlock(this.GetType(), "jScript", jScript);
            }
 
        }
        
        #endregion ..:: PAGE EVENTS ::..

        #region ..:: PRIVATE METHODS ::..

        // What if you wanted to check if a user belongs to a certain security group in Active Directory 
        // or check if user belongs to a SharePoint Group before granting access? 
        // There is no SharePoint permission (SPBasePermission) that directly corresponds to that
        // So, we need to implement our custom logic
        protected virtual void CheckCustomRights()    
        {        
            bool userCheckedForCustomLogic = false;        
            
            // Write here a custom logic to check if user has enough rights 
            // to access application page        
            // If yes, set userCheckedForCustomLogic to true;

            // Let's introduce a super admin here.
            // This page can only be accessed by following user.
            // if (this.Web.CurrentUser.LoginName == "NA\\saodemir")
                userCheckedForCustomLogic = true;  


            if (!userCheckedForCustomLogic)        
            {            
                SPUtility.HandleAccessDenied(new UnauthorizedAccessException());        
            }    
        }

        private void LoadLanguages()
        {
            ddlLanguage.Items.Clear();

            ddlLanguage.Items.Add(new ListItem("Select Language", ""));
            ddlLanguage.Items.Add(new ListItem("Afrikaans", "en|af"));
            ddlLanguage.Items.Add(new ListItem("Albanian", "en|sq"));
            ddlLanguage.Items.Add(new ListItem("Arabic", "en|ar"));
            ddlLanguage.Items.Add(new ListItem("Armenian", "en|hy"));
            ddlLanguage.Items.Add(new ListItem("Azerbaijani", "en|az"));
            ddlLanguage.Items.Add(new ListItem("Basque", "en|eu"));
            ddlLanguage.Items.Add(new ListItem("Belarusian", "en|be"));
            ddlLanguage.Items.Add(new ListItem("Bulgarian", "en|bg"));
            ddlLanguage.Items.Add(new ListItem("Catalan", "en|ca"));
            ddlLanguage.Items.Add(new ListItem("Chinese (Simplified)", "en|zn-CN"));
            ddlLanguage.Items.Add(new ListItem("Chinese (Traditional)", "en|zn-TW"));
            ddlLanguage.Items.Add(new ListItem("Croatian", "en|hr"));
            ddlLanguage.Items.Add(new ListItem("Czech", "en|cs"));
            ddlLanguage.Items.Add(new ListItem("Danish", "en|da"));
            ddlLanguage.Items.Add(new ListItem("Dutch", "en|nl"));
            ddlLanguage.Items.Add(new ListItem("English", "en|en"));
            ddlLanguage.Items.Add(new ListItem("Estonian", "en|et"));
            ddlLanguage.Items.Add(new ListItem("Filipino", "en|tl"));
            ddlLanguage.Items.Add(new ListItem("Finnish", "en|fi"));
            ddlLanguage.Items.Add(new ListItem("French", "en|fr"));
            ddlLanguage.Items.Add(new ListItem("Galician", "en|gl"));
            ddlLanguage.Items.Add(new ListItem("Georgian", "en|ka"));
            ddlLanguage.Items.Add(new ListItem("German", "en|de"));
            ddlLanguage.Items.Add(new ListItem("Greek", "en|el"));
            ddlLanguage.Items.Add(new ListItem("Haitian Creole", "en|ht"));
            ddlLanguage.Items.Add(new ListItem("Hebrew", "en|iw"));
            ddlLanguage.Items.Add(new ListItem("Hindi", "en|hi"));
            ddlLanguage.Items.Add(new ListItem("Hungarian", "en|hu"));
            ddlLanguage.Items.Add(new ListItem("Icelandic", "en|is"));
            ddlLanguage.Items.Add(new ListItem("Indonesian", "en|id"));
            ddlLanguage.Items.Add(new ListItem("Irish", "en|ga"));
            ddlLanguage.Items.Add(new ListItem("Italian", "en|it"));
            ddlLanguage.Items.Add(new ListItem("Japanese", "en|ja"));
            ddlLanguage.Items.Add(new ListItem("Korean", "en|ko"));
            ddlLanguage.Items.Add(new ListItem("Latvian", "en|lv"));
            ddlLanguage.Items.Add(new ListItem("Lithuanian", "en|lt"));
            ddlLanguage.Items.Add(new ListItem("Macedonian", "en|mk"));
            ddlLanguage.Items.Add(new ListItem("Malay", "en|ms"));
            ddlLanguage.Items.Add(new ListItem("Maltese", "en|mt"));
            ddlLanguage.Items.Add(new ListItem("Norwegian", "en|no"));
            ddlLanguage.Items.Add(new ListItem("Persian", "en|fa"));
            ddlLanguage.Items.Add(new ListItem("Polish", "en|pl"));
            ddlLanguage.Items.Add(new ListItem("Portuguese", "en|pt"));
            ddlLanguage.Items.Add(new ListItem("Romanian", "en|ro"));
            ddlLanguage.Items.Add(new ListItem("Russian", "en|ru"));
            ddlLanguage.Items.Add(new ListItem("Serbian", "en|sr"));
            ddlLanguage.Items.Add(new ListItem("Slovak", "en|sk"));
            ddlLanguage.Items.Add(new ListItem("Slovenian", "en|sl"));
            ddlLanguage.Items.Add(new ListItem("Spanish", "en|es"));
            ddlLanguage.Items.Add(new ListItem("Swahili", "en|sw"));
            ddlLanguage.Items.Add(new ListItem("Swedish", "en|sv"));
            ddlLanguage.Items.Add(new ListItem("Thai", "en|th"));
            ddlLanguage.Items.Add(new ListItem("Turkish", "en|tr"));
            ddlLanguage.Items.Add(new ListItem("Ukrainian", "en|uk"));
            ddlLanguage.Items.Add(new ListItem("Urdu", "en|ur"));
            ddlLanguage.Items.Add(new ListItem("Vietnamese", "en|vi"));
            ddlLanguage.Items.Add(new ListItem("Welsh", "en|cy"));
            ddlLanguage.Items.Add(new ListItem("Yiddish", "en|yi"));

        } 
        
        #endregion ..:: PRIVATE METHODS ::..

    }
}
